package com.wnxy.util;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Classname MyRule
 * @Description :
 * @Date 2021/2/24 19:04
 * @Created by Yeheng
 */
public class MyRule extends AbstractLoadBalancerRule {
    private AtomicInteger count;

    public void initWithNiwsConfig(IClientConfig iClientConfig) {
        this.count = new AtomicInteger();
        count.set(0);
    }

    public Server choose(Object o) {

        ILoadBalancer lb = this.getLoadBalancer();
        List<Server> allServers = lb.getAllServers();
        if (count.get()/5>allServers.size()-1) {
            count.set(0);
        }
        int i = count.get()/5;
        count.set(count.get()+1);

        return allServers.get(i);
    }
}
